﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MicropleSoft.WmsServiceLibrary;
using MicropleSoft.WmsServiceLibrary.Domain;
using MicropleSoft.WmsServiceLibrary.Exceptions;
using MicropleSoft.WmsServiceLibrary.Menu;
using MicropleSoft.WmsServiceLibrary.Security;

namespace MicropleSoft.Web
{

    public partial class ServiceFacade : ServiceFacadeBase, IServiceFacade
    {
        private readonly IWmsUserService _wmsUserService = null;
        private readonly IRoleService _roleService = null;
        private readonly IWhConfigService _whConfigService = null;
        private readonly IUserWhService _userWhService = null;
        private readonly IMenuListService _menuListService = null;
        private readonly IAuthentication _authentication = AuthenticationFactory.GetAuthentication();

        public ServiceFacade()
        {
            _wmsUserService = CreateAbstractServiceFactory().CreateWmsUserService();
            _roleService = CreateAbstractServiceFactory().CreateRoleService();
            _whConfigService = CreateAbstractServiceFactory().CreateWhConfigService();
            _userWhService = CreateAbstractServiceFactory().CreateUserWhService();
            _menuListService = CreateAbstractServiceFactory().CreateMenuListService();
        }

        #region IServiceSecurity Members

        public string Login(string userId, string password, string whId)
        {
            return _authentication.Login(userId, password, whId);
        }

        public Dictionary<string, string> GetLoginInfo(string token)
        {
            return new Dictionary<string, string>();
        }

        public WmsUser GetSystemUser(string userId)
        {
            string condition = "and USER_ID ='{0}'";
            condition = string.Format(condition, base.ConvertInjectString(userId));
            var list = _wmsUserService.Query(condition);
            if (list.Count == 0)
                ExceptionHandler.ThrowException(string.Format("用户{0}不存在！",userId));
            return list[0];
        }

        public string ChangePassword(string userId, string oldPassword, string newPassword)
        {
            var wmsUser = GetSystemUser(userId);
            if(CryptoMd5(oldPassword) == wmsUser.Passwd)
            {
                if(AuthenticationFactory.GetAuthentication().GetCurrentUserId() != userId)
                {
                    ExceptionHandler.ThrowException("非当前用户不能修改密码！");
                    return string.Empty;
                }
                else
                {
                    wmsUser.Passwd = CryptoMd5(newPassword);
                    wmsUser.DomainObjectState = DomainEntityState.Updated;
                    _wmsUserService.Save(wmsUser);
                    return "修改成功，请牢记！";
                }
            }
            else
            {
                ExceptionHandler.ThrowException("原始密码错误！");
                return string.Empty;
            }
        }

        public string SetPassword(string userId, string newPassword)
        {
            var wmsUser = GetSystemUser(userId);

            wmsUser.Passwd = CryptoMd5(newPassword);
            wmsUser.DomainObjectState = DomainEntityState.Updated;
            _wmsUserService.Save(wmsUser);
            return "修改成功，请牢记！";
        }

        public void SaveWmsUsers(List<WmsUser> wmsUsers)
        {
            var list = new List<string>();
            _wmsUserService.Save(wmsUsers.ToList<IWmsUser>());
        }

        public void SaveWmsUsers(List<WmsUser> wmsUsers, List<string> notUpdateProperties)
        {
            var list = notUpdateProperties.ToList();
            if ( ! list.Contains("Passwd"))
            {
                list.Add("Passwd");
            }

            _wmsUserService.Save4EncryptPassword(wmsUsers.ToList<IWmsUser>(), list);
        }

        public List<WmsUser> QueryWmsUsers(string condition)
        {
            return _wmsUserService.Query(condition);
        }

        #endregion

        #region Role
        public List<Role> QueryRoles(string condition)
        {
            return _roleService.Query(condition);
        }

        public void SaveRoles(List<Role> roles)
        {
            _roleService.Save(roles.ToList<IRole>());
        }
        #endregion

        public List<MenuItemModel> GetMenuItemModels()
        {
            IMenuBuilder menuBuilder = new MenuBuilder();
            return menuBuilder.GetRootItems();
        }

        public List<WhConfig> GetWhConfigList(string condition)
        {
  
            return _whConfigService.Query(condition);
        }

        public List<MenuList> GetMenuLists(string condition)
        {
            return _menuListService.Query(condition);
        }
    }
}
